package me.luger.project.picc.controller.api;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.dao.cache.redis.springdata.RedisManager;
import me.luger.project.picc.entity.ActivityEnum;
import me.luger.project.picc.entity.PrizeList;
import me.luger.project.picc.service.PrizeListService;
import me.luger.project.picc.service.picc.TongjiService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.picc.service.LearnHealthService;
import me.luger.project.picc.entity.LearnHealth;

import java.util.Map;

/**
 * 学点健康知识吧 controller
 */
@Controller
@RequestMapping("/api/picc/learnHealth")
public class LearnHealthApiController extends BaseController {

    @Autowired
    private LearnHealthService learnHealthService;

    @Autowired
    private PrizeListService prizeListService;

    @Autowired
    private TongjiService tongjiService;

    @Autowired
    private RedisManager redisManager;

    @RequestMapping(value = "/save",name = "添加或者更新学点健康知识吧接口")
    @ResponseBody
    public Object save(String openId,
                       int score){
        int bizId = 0;
        try{
            LearnHealth learnHealth = learnHealthService.fetchByOpenId(openId);
            if(null == learnHealth){
                learnHealth = new LearnHealth();
                learnHealth.setOpenId(openId);
                learnHealth.setScore(score);
                if(StringUtil.isBlank(openId)){
                    learnHealth.setOpenId("luger"+System.currentTimeMillis());
                    learnHealth.setDoPrizeStatus(0);
                }else{
                    learnHealth.setDoPrizeStatus(1);
                }
                learnHealth = learnHealthService.insert(learnHealth);
            }
            tongjiService.addUserCount(openId, ActivityEnum.LEARN_HEALTH,0);
            bizId = learnHealth.getId();
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("bizId",bizId);
    }

    @RequestMapping(value = "/status",name = "用户是否参加客户满意度有奖调查抽奖")
    @ResponseBody
    public Object status(String openId){
        Map<String,Object> result = new HashedMap();
        boolean hasSubmitQuestion = false,hasDoDraw=false,hasGetPrize=false,hasCompleteAdress=false;
        int bizId = 0;
        String prizeSecrect = "";
        try{
            LearnHealth learnHealth = learnHealthService.fetchByOpenId(openId);
            if(null != learnHealth){
                bizId = learnHealth.getId();
                hasSubmitQuestion=true;
            }

            PrizeList prizeList = prizeListService.fetchByOpenIdAndType(openId, ActivityEnum.LEARN_HEALTH.getActivityId());

            //用户是否抽奖
            if(null != prizeList){
                hasDoDraw = true;
            }

            //如果用户抽过奖返回用户是否中奖。
            if(hasDoDraw && prizeList.getPrizeResult() == 1){
                hasGetPrize = true;
            }

            if(hasGetPrize && StringUtil.isNotBlank(prizeList.getUserName()) && StringUtil.isNotBlank(prizeList.getPhoneNum())){
                hasCompleteAdress = true;
            }

            if(hasGetPrize && !hasCompleteAdress){
                prizeSecrect = prizeList.getPrizeSecrect();
            }

            result.put("hasSubmitQuestion",hasSubmitQuestion);
            result.put("hasDoDraw",hasDoDraw);
            result.put("hasGetPrize",hasGetPrize);
            result.put("hasCompleteAdress",hasCompleteAdress);
            result.put("bizId",bizId);
            result.put("prizeSecrect",prizeSecrect);

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success(result);
    }

    @RequestMapping(value = "saveQuestion",name = "根据openId存储答题接口")
    @ResponseBody
    public Object saveQuestion(String openId,String question){
        try{
            redisManager.set("userQuestion:"+openId,question);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "getQuestion",name = "根据openId读取答题接口")
    @ResponseBody
    public Object getQuestion(String openId){
        String question = "";
        try{
            question = redisManager.get("userQuestion:"+openId);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("question",question);
    }

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }
}
